package com.seven.micro_service.controller;

import com.seven.micro_service.tools.JwtUtil;
import com.seven.micro_service.service.LoginService;
import com.seven.micro_service.tools.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

    private JwtUtil jwtUtil;

    @Autowired
    public void setJwtUtil(JwtUtil jwtUtil) {
        this.jwtUtil = jwtUtil;
    }

    private LoginService loginService;
    @Autowired
    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }

    @GetMapping(value = "login")
    public Result<String> doLogin(@RequestParam(value = "id") String id,
                                  @RequestParam(value = "pwd") String pwd) {
        //与数据库比较
        if (loginService.isAllowedLogin(id, pwd)) {
            int EXPIRE = 1000 * 300;//过期时间 单位毫秒
            String subject = "A";  //发布者
            String token = jwtUtil.createJWT(id, subject, EXPIRE);//生成用户令牌

            return new Result<>(2000, "success", token);
        }
        return new Result<>(1001, "The user does not exist or the password is wrong!");
    }

}
